﻿/*
   Copyright 2013 Dmitry Bratus

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Flower.Directory.Host.Default;
using Flower.Services;
using NUnit.Framework;
using Jurassic;
using Flower.Directory.Management.Scripting;
using System.IO;
using Jurassic.Library;
using Flower.Directory.Scripting;
using Flower.Testing;

namespace Flower.Directory.Management.Tests
{
    [TestFixture]
	public class ManagementUtilitiesTests
    {
        private ScriptEngine _engine;
        private IDirectory _directory;

        [SetUp]
        public void Init()
        {
            _engine = Scripting.Setup.InitEngine(Directory.Scripting.Setup.CreateEngine());

            System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
            string[] resourceNames = assembly.GetManifestResourceNames();

            var scripts = new StringBuilder();

            foreach (string resourceName in resourceNames)
            {
                if (resourceName.StartsWith("Flower.Directory.Management.Tests.Scripts"))
                {
                    var stream = assembly.GetManifestResourceStream(resourceName);

                    if (stream != null)
                    {
                        using (var reader = new StreamReader(stream, Encoding.UTF8))
                        {
                            scripts.Append(reader.ReadToEnd());
                        }
                    }
                }
            }

            _directory = new MemoryDirectory.Directory(new ScriptEnginePool(1));
            _engine.SetGlobalValue("$dir", new DirectoryObject(_engine, _directory));

            _engine.Execute(scripts.ToString());
        }

        [Test]
        public void CdTest()
        {
            var result = (ObjectInstance)_engine.Evaluate("cdTest()");

            Assert.IsTrue((bool)result["initialIsRoot"]);
            Assert.IsTrue((bool)result["returnsSelf"]);
            Assert.IsTrue((bool)result["pathIsMapped"]);
            Assert.IsTrue((bool)result["globalIsGlobal"]);
            Assert.IsTrue((bool)result["idIsId"]);
        }

        [Test]
        public void GetTest()
        {
            var result = (ObjectInstance)_engine.Evaluate("getTest()");

            Assert.IsTrue((bool)result["get"]);
            Assert.IsTrue((bool)result["getPayload"]);
            Assert.IsTrue((bool)result["getInfo"]);
        }

        [Test]
        public void DirTest()
        {
            var result = (ObjectInstance)_engine.Evaluate("dirTest()");

            Assert.IsTrue((bool)result["atRoot3"]);
            Assert.IsTrue((bool)result["roles1"]);
            Assert.IsTrue((bool)result["workflowsAsc"]);
            Assert.IsTrue((bool)result["noWorkflows"]);
            Assert.IsTrue((bool)result["allWorkflows"]);
            Assert.IsTrue((bool)result["allWorkflowsWithPayload"]);
        }

        [Test]
        public void CountMkdirRmRenameTest()
        {
            var result = (ObjectInstance)_engine.Evaluate("countMkdirRmRenameTest()");

            Assert.IsTrue((bool)result["dirCreated"]);
            Assert.IsTrue((bool)result["dirRenamed"]);
            Assert.IsTrue((bool)result["count0"]);
            Assert.IsTrue((bool)result["count1"]);
            Assert.IsTrue((bool)result["count2"]);
            Assert.IsTrue((bool)result["count3"]);
        }

        [Test]
        public void GrantRevokeTest()
        {
            var result = (ObjectInstance)_engine.Evaluate("grantRevokeTest()");

            Assert.IsTrue((bool)result["initialPermissions"]);
            Assert.IsTrue((bool)result["permissions1"]);
            Assert.IsTrue((bool)result["permissions2"]);
        }

        [Test]
        public void LnTest()
        {
            var result = (ObjectInstance)_engine.Evaluate("lnTest()");

            Assert.IsTrue((bool)result["byPath"]);
            Assert.IsTrue((bool)result["byId"]);
        }

        [Test]
        public void CreateProcessorTest()
        {
            var result = (ObjectInstance)_engine.Evaluate("createProcessorTest()");

            Assert.IsTrue((bool)result["created"]);
            Assert.IsTrue((bool)result["pending"]);
            Assert.IsTrue((bool)result["running"]);
            Assert.IsTrue((bool)result["waiting"]);
            Assert.IsTrue((bool)result["suspended"]);
            Assert.IsTrue((bool)result["broken"]);
            Assert.IsTrue((bool)result["finished"]);
            Assert.IsTrue((bool)result["processorAcl"]);
            Assert.IsTrue((bool)result["pendingAcl"]);
            Assert.IsTrue((bool)result["runningAcl"]);
            Assert.IsTrue((bool)result["waitingAcl"]);
            Assert.IsTrue((bool)result["suspendedAcl"]);
            Assert.IsTrue((bool)result["brokenAcl"]);
            Assert.IsTrue((bool)result["finishedAcl"]);
        }

        [Test]
        public void UpdateProcessorTest()
        {
            var result = (ObjectInstance)_engine.Evaluate("updateProcessorTest()");

            Assert.IsTrue((bool)result["created"]);
            Assert.IsTrue((bool)result["pending"]);
            Assert.IsTrue((bool)result["running"]);
            Assert.IsTrue((bool)result["waiting"]);
            Assert.IsTrue((bool)result["suspended"]);
            Assert.IsTrue((bool)result["broken"]);
            Assert.IsTrue((bool)result["finished"]);
            Assert.IsTrue((bool)result["processorAcl"]);
            Assert.IsTrue((bool)result["pendingAcl"]);
            Assert.IsTrue((bool)result["runningAcl"]);
            Assert.IsTrue((bool)result["waitingAcl"]);
            Assert.IsTrue((bool)result["suspendedAcl"]);
            Assert.IsTrue((bool)result["brokenAcl"]);
            Assert.IsTrue((bool)result["finishedAcl"]);
        }

        [Test]
        public void CreateUpdateRoleTest()
        {
            var result = (ObjectInstance)_engine.Evaluate("createUpdateRoleTest()");

            Assert.IsTrue((bool)result["create"]);
            Assert.IsTrue((bool)result["update"]);
            Assert.IsTrue((bool)result["add"]);
            Assert.IsTrue((bool)result["remove"]);
        }

        [Test]
        public void CreateUpdateSetTest()
        {
            var result = (ObjectInstance)_engine.Evaluate("createUpdateSetTest()");

            Assert.IsTrue((bool)result["create"]);
            Assert.IsTrue((bool)result["createMessageType"]);
            Assert.IsTrue((bool)result["createCapacity"]);
            Assert.IsTrue((bool)result["updateMessageType"]);
            Assert.IsTrue((bool)result["updateCapacity"]);
        }

        [Test]
        public void CreateUpdateWorkflowTest()
        {
            var result = (ObjectInstance)_engine.Evaluate("createUpdateWorkflowTest()");

            Assert.IsTrue((bool)result["create"]);
            Assert.IsTrue((bool)result["createDefinitionType"]);
            Assert.IsTrue((bool)result["createPreferredProcessors"]);
            Assert.IsTrue((bool)result["updateDefinitionType"]);
            Assert.IsTrue((bool)result["updatePreferredProcessors"]);
        }

        [Test]
        public void RetryProcessTest()
        {
            var result = (ObjectInstance)_engine.Evaluate("retryProcessTest()");

            Assert.IsTrue((bool)result["retried"]);
        }
    }
}